package com.dragon.client.controller.auth;

import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.contract.ContractService;
import com.dragon.logical.service.customer.CustomerService;
import com.dragon.logical.service.finance.*;
import com.dragon.repository.entity.DictsortDetail;
import com.dragon.repository.entity.Expenses;
import com.dragon.repository.entity.PaymentObject;
import com.dragon.repository.entity.PlanCustomerPayment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务统计管理
 * @author Charlotte
 */
@RestController
@RequestMapping("statistics")
@Api(tags = "统计管理")
public class StatisticsController {

    @Autowired
    ContractService contractService;
    @Autowired
    CustomerService customerService;
    @Autowired
    IPlanCollectService planCollectService;
    @Autowired
    ICustomerPaymentService customerPaymentService;
    @Autowired
    ILoanService loanService;
    @Autowired
    IReimbursementService reimbursementService;
    @Autowired
    IPaymentService paymentService;

    @GetMapping("contractStatistics")
    @ApiOperation(value = "业务统计信息")
    public ResponseData contractStatistics(){
        return ResponseData.success(contractService.businessStatistics());
    }

    @GetMapping("financeStatistics")
    @ApiOperation(value = "财务统计信息")
    public ResponseData financeStatistics(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        return ResponseData.success(planCollectService.financeTotal(time));
    }

    @GetMapping("planTotalStatistics")
    @ApiOperation(value = "应收总额统计")
    public ResponseData planTotalStatistics(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        return ResponseData.success(planCollectService.planMonthlyStatistics(time));
    }

    @GetMapping("customerPaymentTotalStatistics")
    @ApiOperation(value = "实收总额统计")
    public ResponseData customerPaymentTotalStatistics(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        LinkedHashMap<String, Object> paymentMonthlyStatistics = customerPaymentService.customerPaymentMonthlyStatistics(time);
        return ResponseData.success(paymentMonthlyStatistics);
    }

    @GetMapping("financeStatist")
    @ApiOperation(value = "应收实收月统计")
    public ResponseData financeStatist(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        List<PlanCustomerPayment> objectList = planCollectService.planCustomerPaymentStatist(time);
        return ResponseData.success(objectList);
    }

    @GetMapping("totalPercentage")
    @ApiOperation(value = "总额占比")
    public ResponseData totalPercentage(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        Map<String,Object> percentage = planCollectService.totalPercentage(time);
        return ResponseData.success(percentage);
    }

    @GetMapping("loanPercentage")
    @ApiOperation(value = "借款类型占比")
    public ResponseData loanTypePercentage(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        List<DictsortDetail> loanPercentage = loanService.loanTypePercentage(time);
        return ResponseData.success(loanPercentage);
    }

    @GetMapping("loanStatistics")
    @ApiOperation(value = "借款月总额统计")
    public ResponseData loanTotalStatistics(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        return ResponseData.success(loanService.loanMonthlyStatistics(time));
    }

    @GetMapping("reimbursePercentage")
    @ApiOperation(value = "报销类型占比")
    public ResponseData reimburseTypePercentage(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        List<Expenses> reimbursePercentage = reimbursementService.reimburseTypePercentage(time);
        return ResponseData.success(reimbursePercentage);
    }

    @GetMapping("reimburseStatistics")
    @ApiOperation(value = "报销月总额统计")
    public ResponseData reimburseTotalStatistics(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        return ResponseData.success(reimbursementService.reimburseMonthlyStatistics(time));
    }

    @GetMapping("paymentTypePercentage")
    @ApiOperation(value = "付款类型占比")
    public ResponseData paymentTypePercentage(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        List<PaymentObject> paymentPercentage = paymentService.paymentTypePercentage(time);
        return ResponseData.success(paymentPercentage);
    }

    @GetMapping("paymentStatistics")
    @ApiOperation(value = "付款月总额统计")
    public ResponseData paymentTotalStatistics(
            @ApiParam(name = "time", value = "年份", required = false, type = "int")
            @RequestParam(required = false) String time){
        LinkedHashMap<String, Object> paymentPercentage = paymentService.paymentTotalStatistics(time);
        return ResponseData.success(paymentPercentage);
    }
}
